package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.Defect;
import org.example.mapper.DefectMapper;
import org.example.service.DefectService;
import org.example.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 严伟伦
 * @Company 无
 * @Description
 * @Date: 2024/10/16 19:14
 */
@Service
public class DefectServiceImpl implements DefectService {
    @Autowired
    private DefectMapper defectMapper;
    @Override
    public MESResult queryByPage(Defect defect, Integer page, Integer limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小,这一条下面第一个方法就是分页方法
        PageHelper.startPage(page,limit);
        // 查询全部
        List<Defect> defects = defectMapper.selectByPage(defect);

        // 创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Defect> defectPageInfo = new PageInfo<>(defects);

        return MESResult.success(defectPageInfo);
    }

    @Override
    public MESResult addDefect(Defect defect) {

        if (defectMapper.addDefect(defect) == 1){
            return MESResult.success(defect);
        }
        return MESResult.error();
    }

    @Override
    public MESResult updateDefect(Defect defect) {
        if (defect == null){
            return MESResult.error();
        }
        if (defectMapper.updateDefect(defect) == 1){
            return MESResult.success();
        }
        return MESResult.error();
    }

    @Override
    public MESResult selectDefectById(int defectId) {
        Defect defects = defectMapper.selectDefectById(defectId);
        return MESResult.success(defects);
    }

    @Override
    public MESResult deleteDefectById(int defectId) {
        if (defectMapper.deleteDefectById(defectId) == 1){
            return MESResult.success();
        }
        return MESResult.error();
    }

    @Override
    public MESResult deletes(int[] defectIds) {
        return MESResult.success(200,"OK",defectMapper.deletes(defectIds));
    }
}
